CLASS lhc_ZI_SCHEDGLINEAPI_C DEFINITION INHERITING FROM cl_abap_behavior_handler.
  PRIVATE SECTION.

    METHODS get_instance_authorizations FOR INSTANCE AUTHORIZATION
      IMPORTING keys REQUEST requested_authorizations FOR zi_schedglineapi_c RESULT result.

    METHODS update FOR MODIFY
      IMPORTING entities FOR UPDATE zi_schedglineapi_c.

    METHODS read FOR READ
      IMPORTING keys FOR READ zi_schedglineapi_c RESULT result.

    METHODS lock FOR LOCK
      IMPORTING keys FOR LOCK zi_schedglineapi_c.
    METHODS create FOR MODIFY
      IMPORTING entities FOR CREATE zi_schedglineapi_c.
    METHODS get_global_authorizations FOR GLOBAL AUTHORIZATION
      IMPORTING REQUEST requested_authorizations FOR zi_schedglineapi_c RESULT result.

ENDCLASS.

CLASS lhc_ZI_SCHEDGLINEAPI_C IMPLEMENTATION.

  METHOD get_instance_authorizations.
    IF requested_authorizations-%update = if_abap_behv=>mk-on.



    ENDIF.
  ENDMETHOD.

  METHOD update.
  ENDMETHOD.

  METHOD read.
  ENDMETHOD.

  METHOD lock.
  ENDMETHOD.


  METHOD create.

    DATA: lt_schedline_update TYPE TABLE FOR UPDATE I_SchedgLineTP_2.
    DATA: lt_schedgline_read TYPE TABLE FOR READ IMPORT I_SchedgLineTP_2.

    DATA lv_message TYPE string.
    data lv_errortext TYPE string.
    DATA lv_severity TYPE if_abap_behv_message=>t_severity.


    lt_schedgline_read = CORRESPONDING #( entities ).
*
    READ ENTITY PRIVILEGED I_SchedgLineTP_2 ALL FIELDS WITH lt_schedgline_read
    RESULT DATA(lt_schd_line)
          FAILED DATA(ls_read_failed)
          REPORTED DATA(ls_read_reported).

    LOOP AT entities INTO DATA(ls_entities).

      CHECK  lt_schd_line IS NOT INITIAL.

      READ TABLE lt_schd_line INTO DATA(ls_schd_line) WITH KEY
                 SchedulingAgreement = ls_entities-SchedulingAgreement
                 SchedulingAgreementItem    = ls_entities-SchedulingAgreementItem
                 ScheduleLine               = ls_entities-ScheduleLine.

      CHECK ls_schd_line IS NOT INITIAL.

      APPEND INITIAL LINE TO lt_schedline_update ASSIGNING FIELD-SYMBOL(<ls_schedline_upd>).

      <ls_schedline_upd>-SchedulingAgreement                = ls_schd_line-SchedulingAgreement.
      <ls_schedline_upd>-SchedulingAgreementItem            = ls_schd_line-SchedulingAgreementItem.
      <ls_schedline_upd>-ScheduleLine                       = ls_schd_line-ScheduleLine.
      <ls_schedline_upd>-ScheduleLineOrderQuantity          = ls_entities-ScheduleLineOrderQuantity.
      <ls_schedline_upd>-ScheduleLineDeliveryDate           = ls_entities-ScheduleLineDeliveryDate.
      <ls_schedline_upd>-%control-ScheduleLineOrderQuantity = if_abap_behv=>mk-on.
      <ls_schedline_upd>-%control-ScheduleLineDeliveryDate  = if_abap_behv=>mk-on.


      MODIFY ENTITIES OF I_SchedgAgrmtHdrTP_2 PRIVILEGED
        ENTITY PurSchedgAgrmtItmScheduleLine
          UPDATE FROM lt_schedline_update
            MAPPED DATA(ls_mapped)
            FAILED DATA(ls_failed)
            REPORTED DATA(ls_reported).

      lv_errortext = cond #( when ls_reported-purschedgagrmtitmscheduleline is not INITIAL
                                   then ls_reported-purschedgagrmtitmscheduleline[ 1 ]-%msg->if_message~get_text( )
                                   else '运行时错误' ).

      lv_message = |{ ls_entities-SchedulingAgreement }/{ ls_entities-SchedulingAgreementItem }/{ ls_entities-ScheduleLine }:|.

      lv_message = lv_message &&  COND #( WHEN ls_failed IS INITIAL THEN |更新成功|
                              ELSE lv_errortext ).

      lv_severity = COND #( WHEN ls_failed IS INITIAL THEN if_abap_behv_message=>severity-success
                           ELSE if_abap_behv_message=>severity-error ).

*      IF lv_severity = if_abap_behv_message=>severity-success.
*        APPEND VALUE #( %tky = ls_schd_line-%tky ) TO mapped-zi_schedglineapi_c.
*      ELSE.
*        APPEND VALUE #( %tky = ls_entities-%tky ) TO failed-zi_schedglineapi_c.
*      ENDIF.

      APPEND VALUE #( "%tky = ls_entities-%key
                      %msg = new_message( id = '00'
                                          number   = '001'
                                          severity = lv_severity
                                          v1 = lv_message ) ) TO reported-zi_schedglineapi_c.

      CLEAR: lt_schedline_update, ls_failed, ls_reported, lv_errortext, lv_message.
    ENDLOOP.
  ENDMETHOD.
  METHOD get_global_authorizations.
    IF requested_authorizations-%update = '01'.

      result-%update = if_abap_behv=>auth-unauthorized.
      APPEND VALUE #( %msg      = new_message_with_text(
                      severity = if_abap_behv_message=>severity-error
                      text = 'operation not authorized!' ) )
                      TO reported-zi_schedglineapi_c.

    ENDIF.
  ENDMETHOD.


ENDCLASS.

CLASS lsc_ZI_SCHEDGLINEAPI_C DEFINITION INHERITING FROM cl_abap_behavior_saver.
  PROTECTED SECTION.

    METHODS finalize REDEFINITION.

    METHODS check_before_save REDEFINITION.

    METHODS save REDEFINITION.

    METHODS cleanup REDEFINITION.

    METHODS cleanup_finalize REDEFINITION.

ENDCLASS.

CLASS lsc_ZI_SCHEDGLINEAPI_C IMPLEMENTATION.

  METHOD finalize.
  ENDMETHOD.

  METHOD check_before_save.
  ENDMETHOD.

  METHOD save.
  ENDMETHOD.

  METHOD cleanup.
  ENDMETHOD.

  METHOD cleanup_finalize.
  ENDMETHOD.

ENDCLASS.
